﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using System.Resources;
using University.Workshop.Model;

namespace University.Workshop.WinApp
{
    public partial class PrincipalForm : TemplateForm
    {
        DateTime firstDate;
        int idType = 0;
        private ResourceManager resours;
        int year = 0;
        string firstSelecctionDate;
        string lastSelecctionDate;
        string roomRow;
        public PrincipalForm()
        {
            InitializeComponent();
            resours = new ResourceManager(typeof(PrincipalForm));
        }

        private void PrincipalForm_Load(object sender, EventArgs e)
        {
            actualDate.Text = DateTime.Today.ToShortDateString();
            //userlabel.Text = "aca";
            userlabel.Text = Program.actualUser.Name + " " + Program.actualUser.Surname;

            roomsDataGridView.Rows.Clear();
            firstDate = DateTime.Today;
            year = firstDate.Year;
            RoomStuffManager.GetAllBookedAndTokenRoomsForAWeek(DateTime.Today, roomsDataGridView, 0);
            this.StyleTheCells();
            this.comboBoxTypes.SelectedIndex = 4;
        }

        public void StyleTheCells()
        {
            String[] listName = RoomStuffManager.CreateColumnsName(firstDate);
            int tamRows = roomsDataGridView.Rows.Count;
            for (int listIterator = 0; listIterator < 7; listIterator++)
            {
                for (int rowIterator = 0; rowIterator < tamRows; rowIterator++)
                {
                    if (roomsDataGridView[listName[listIterator], rowIterator].Value.ToString() == "Taken")
                    {
                        roomsDataGridView[listName[listIterator], rowIterator].Style.BackColor = Color.Red;
                    }
                    if (roomsDataGridView[listName[listIterator], rowIterator].Value.ToString() == "Booked")
                    {
                        roomsDataGridView[listName[listIterator], rowIterator].Style.BackColor = Color.Salmon;
                    }
                    if (roomsDataGridView[listName[listIterator], rowIterator].Value.ToString() == "Free")
                    {
                        roomsDataGridView[listName[listIterator], rowIterator].Style.BackColor = Color.Aquamarine;
                    }
                }
            }
        }

        private void comboBoxTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            String roomType = (String)comboBoxTypes.Text.Trim();
            idType = RoomStuffManager.GetByName(roomType);
            roomsDataGridView.Rows.Clear();
            roomsDataGridView.Columns.Clear();
            RoomStuffManager.GetAllBookedAndTokenRoomsForAWeek(firstDate, roomsDataGridView, idType);
            this.StyleTheCells();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            roomsDataGridView.Rows.Clear();
            roomsDataGridView.Columns.Clear();
            firstDate = firstDate.AddDays(7);
            year = firstDate.Year;
            RoomStuffManager.GetAllBookedAndTokenRoomsForAWeek(firstDate, roomsDataGridView, idType);
            this.StyleTheCells();
        }

        private void label4_Click(object sender, EventArgs e)
        {
            roomsDataGridView.Rows.Clear();
            roomsDataGridView.Columns.Clear();
            firstDate = firstDate.AddDays(-7);
            year = firstDate.Year;
            RoomStuffManager.GetAllBookedAndTokenRoomsForAWeek(firstDate, roomsDataGridView, idType);
            this.StyleTheCells();
        }

        private void checkInToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int month = Convert.ToInt32(lastSelecctionDate.Substring(3));
                int day = Convert.ToInt32(lastSelecctionDate.Substring(0, 2));
                CheckInForm checkInForms = new CheckInForm(roomRow, new DateTime(year, month, day), true);
                checkInForms.ShowDialog();
                roomsDataGridView.Rows.Clear();
                roomsDataGridView.Columns.Clear();
                RoomStuffManager.GetAllBookedAndTokenRoomsForAWeek(firstDate, roomsDataGridView, idType);
                this.StyleTheCells();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bookingRoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                int firstMonth = Convert.ToInt32(this.firstSelecctionDate.Substring(3));
                int firstDay = Convert.ToInt32(this.firstSelecctionDate.Substring(0, 2));
                int month = Convert.ToInt32(lastSelecctionDate.Substring(3));
                int day = Convert.ToInt32(lastSelecctionDate.Substring(0, 2));
                BookingRoomForm bookingRoom = new BookingRoomForm(new DateTime(this.year, firstMonth, firstDay), new DateTime(this.year, month, day), this.roomRow);
                bookingRoom.ShowDialog();
                roomsDataGridView.Rows.Clear();
                roomsDataGridView.Columns.Clear();
                RoomStuffManager.GetAllBookedAndTokenRoomsForAWeek(firstDate, roomsDataGridView, idType);
                this.StyleTheCells();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void roomsDataGridView_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (!(this.roomsDataGridView.Rows.Count - 1 > 0) && !(this.roomsDataGridView.SelectedRows.Count > 1))
            {
                string caption = this.resours.GetString("messageBox.Caption");
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, "Rows Selection Is Invalid", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
            else
            {
                try
                {
                    int cuenta = this.roomsDataGridView.SelectedCells.Count;
                    this.lastSelecctionDate = this.roomsDataGridView.Columns[e.ColumnIndex].HeaderText;
                    this.roomRow = this.roomsDataGridView.Rows[e.RowIndex].Cells[0].Value.ToString();
                    if (this.lastSelecctionDate == "Rooms") { this.lastSelecctionDate = this.roomsDataGridView.Columns[e.ColumnIndex + 1].HeaderText; }
                    this.firstSelecctionDate = this.roomsDataGridView.Columns[e.ColumnIndex - cuenta + 1].HeaderText;
                    if (this.firstSelecctionDate == "Rooms") { this.firstSelecctionDate = this.roomsDataGridView.Columns[e.ColumnIndex - cuenta + 2].HeaderText; }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Solo se puede seleccionar una fila o parte de una fila " + ex.Message);
                }
                // MessageBox.Show(firstSelecctionDate + " " + lastSelecctionDate + " " + this.roomRow +" "+ cuenta);
            }
        }


        private void roomsDataGridView_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (!(this.roomsDataGridView.Rows.Count - 1 > 0) && !(this.roomsDataGridView.SelectedRows.Count > 1))
            {
                string caption = this.resours.GetString("messageBox.Caption");
                MessageBoxButtons buttons = MessageBoxButtons.OK;
                MessageBox.Show(this, "Rows Selection Is Invalid", caption, buttons, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);
            }
            else
            {
                if (this.roomsDataGridView[e.ColumnIndex, e.RowIndex].Value.ToString() != "Free")
                {
                    String state = this.roomsDataGridView[e.ColumnIndex, e.RowIndex].Value.ToString();
                    string roomName = this.roomsDataGridView[0, e.RowIndex].Value.ToString();
                    string headerText = this.roomsDataGridView.Columns[e.ColumnIndex].HeaderText;
                    DateTime selectionDate = new DateTime();
                    int month = Convert.ToInt32(headerText.Substring(3));
                    int day = Convert.ToInt32(headerText.Substring(0, 2));
                    selectionDate = new DateTime(this.year, month, day);
                    try
                    {
                        Information info = PrincipalManager.GetAllInfo(roomName, selectionDate, state);
                        String message = "Nombre: \t" + info.ClientInfo.FullName + "\tCI: " + info.ClientInfo.ClientId +
                                         "\nTiempo: " + info.CheckIn.ToShortDateString() + " - " + info.Checkout.ToShortDateString()
                                         + "\nHabitacion: " + info.RoomInfo.Name
                                         + "\nTipo De Habitacion: " + info.RoomInfo.TypesName;
                        if (state == "Taken") { message += "\nParcial de Consumo: " + info.CostSaleProducts.ToString(); }
                        message += "\n\n\t" + state;
                        MessageBox.Show(message, "Info", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error En La Informacion " + ex.Message);
                    }
                }
            }
        }





    }
}
